package dk.ucn.datamatiker.jmaweather;

import java.net.URI;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;

import android.R.integer;
import android.R.string;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Xml;

public class WeatherData
{

	private LocationData _location;
	private String _updateTime;
	private String _description;
	private double _temperature;
	private String _temperatureUnit;
	private String _windDirection;
	private double _windSpeed;
	private String _windSpeedUnit;
	private Bitmap _icon;

	private WeatherData(LocationData location, String updateTime, String description, double temperature, String temperatureUnit, String windDirection, double windSpeed,
			String windSpeedUnit, Bitmap icon)
	{
		_location = location;
		_updateTime = updateTime;
		_description = description;
		_temperature = temperature;
		_temperatureUnit = temperatureUnit;
		_windDirection = windDirection;
		_windSpeed = windSpeed;
		_windSpeedUnit = windSpeedUnit;
		_icon = icon;
	}
	
	public LocationData getLocation()
	{
		return _location;
	}

	public String getUpdateTime()
	{
		return _updateTime;
	}

	public String getDescription()
	{
		return _description;
	}

	public double getTemperature()
	{
		return _temperature;
	}

	public String getTemperatureUnit()
	{
		return _temperatureUnit;
	}

	public String getWindDirection()
	{
		return _windDirection;
	}

	public double getWindSpeed()
	{
		return _windSpeed;
	}

	public String getWindSpeedUnit()
	{
		return _windSpeedUnit;
	}

	public Bitmap getIcon()
	{
		return _icon;
	}
	
	public static WeatherData fromCity(String cityName)
	{

		WeatherData weatherData = null;
		LocationData locationData = LocationData.fromCity(cityName);

		if (locationData != null)
		{

			try
			{

				URI getWeatherUri = new URI("http://balder.ucn.dk/weather/Weather.asmx/GetWeather?loc=" + locationData.getId());

				HttpClient httpClient = new DefaultHttpClient();
				HttpGet pageGet = new HttpGet(getWeatherUri);
				HttpResponse response = httpClient.execute(pageGet);

				XmlPullParser parser = Xml.newPullParser();
				parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
				parser.setInput(response.getEntity().getContent(), null);

				boolean foundWeather = false;

				// Parse xml data until we find matching city or no city at all
				while ((parser.next() != XmlPullParser.END_DOCUMENT) && (!foundWeather))
				{

					if (parser.getEventType() == XmlPullParser.START_TAG)
					{

						if (parser.getName().equals("WeatherData"))
						{

							String locationId = "";
							String updateTime = "";
							String description = "";
							double temperature = 0;
							String temperatureUnit = "";
							String windDirection = "";
							double windSpeed = 0;
							String windSpeedUnit = "";
							String iconUrl = "";

							while ((parser.nextTag() != XmlPullParser.END_TAG))
							{

								String currentTag = parser.getName();

								if (currentTag.equals("Location"))
								{
									locationId = parser.nextText();
								}
								else if (currentTag.equals("UpdateTime"))
								{
									updateTime = parser.nextText();
								}
								else if (currentTag.equals("Description"))
								{
									description = parser.nextText();
								}
								else if (currentTag.equals("Temperature"))
								{
									temperature = Double.parseDouble(parser.nextText());
								}
								else if (currentTag.equals("TemperatureUnit"))
								{
									temperatureUnit = parser.nextText();
								}
								else if (currentTag.equals("WindDirection"))
								{
									windDirection = parser.nextText();
								}
								else if (currentTag.equals("WindSpeed"))
								{
									windSpeed = Double.parseDouble(parser.nextText());
								}
								else if (currentTag.equals("WindSpeedUnit"))
								{
									windSpeedUnit = parser.nextText();
								}
								else if (currentTag.equals("IconUrl"))
								{
									iconUrl = parser.nextText();
								}
								else
								{
									parser.nextText();
								}

							}
							
							Bitmap icon = null;

							if (iconUrl.length() > 0)
							{
								URL iconUrlFull = new URL("http://deskwx.weatherbug.com/images/Forecast/icons/" + iconUrl);
								icon = BitmapFactory.decodeStream(iconUrlFull.openStream());
							}

							weatherData = new WeatherData(locationData, updateTime, description, temperature, temperatureUnit, windDirection, windSpeed, windSpeedUnit, icon);
							foundWeather = true;
							
						}

					}

				}

			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

		}

		return weatherData;

	}

}
